package TUILayer;//makes this class a part of the TUILayer package

import java.util.ArrayList;

import ControlLayer.AddressBookController;
import ModelLayer.Person;
 

/**
 * User interface class to the AddressBook
 * 
 * @author (kbh) 
 * @version (2005.03.31)
 * (FEN): 2006.03.21: Comments translated into English
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 */
public class AddressBookUI extends BaseUI {

    private AddressBookController controller;
    
    private static final int CHOICE_CREATE = 1;
    private static final int CHOICE_SEARCH = 2;
    private static final int CHOICE_LIST = 3;
    private static final int CHOICE_BACK = 0;
    
    private static final int PERSON_EDIT = 1;
    private static final int PERSON_DELETE = 2;
    private static final int PERSON_BACK = 0;
    
    private static final int EDIT_NAME = 1;
    private static final int EDIT_ADDRESS = 2;
    private static final int EDIT_ZIPCODE = 3;
    private static final int EDIT_CITY = 4;
    private static final int EDIT_PHONE = 5;
    private static final int EDIT_BACK = 0;
    
    public AddressBookUI() {
       controller = AddressBookController.getInstance();
    }
    
    public void start() {
        printMenu();
    }
    
    private void printMenu() {
        
        boolean exit = false;
        while(!exit) {
            
            System.out.println("\n*** Adresse Bog Menu ***");
            System.out.println("" + CHOICE_CREATE + ") Opret person");
            System.out.println("" + CHOICE_SEARCH + ") Søg efter person");
            System.out.println("" + CHOICE_LIST + ") Vis alle personer i addressebogen");
            System.out.println("" + CHOICE_BACK + ") Tilbage til hovedmenuen");

            System.out.println("\nIndtast valg: ");
            
            int choice = inputInt();
            
            System.out.println();
        
            switch(choice) {
                case CHOICE_CREATE: 
                    createPerson();
                    break;
                case CHOICE_SEARCH:
                    findPerson();
                    break;
                case CHOICE_LIST:
                    listPersons();
                    break;
                case CHOICE_BACK:
                    exit = true;
                default:
                    System.out.println("Indtast et gyldigt valgt.");
                    break;
            }
        }
    }
    
    private void listPersons() {
        ArrayList<Person> list = controller.getAllPersons();

        if(list.size() > 0) {
            System.out.println("== Alle personer i addressebog ==");
            Person person = selectPerson(list);
            personMenu(person);
        } else {
            System.out.println("Der er ingen personer i addressebogen");
        }

    }
    
    private void findPerson() {
        // prompt user
        System.out.println("Indtast søgekriterie");
        
        // input from user
        String query = inputString();
        
        // search for user
        ArrayList<Person> results = controller.search(query);
        
        // select person from list of results
        Person person = selectPerson(results);
        
        // show person menu
        printPerson(person);
        personMenu(person);
        
    }
    
    private Person selectPerson(ArrayList<Person> list) {
        
        // print
        printPersonList(list);
        
        
        System.out.print("Vælg person: ");
        // prompt
        int choice = inputInt();
        
        // selected number is greater than number of items or less than 1
        if(choice > (list.size() - 1) || choice < 1) {
            // invalid choice
            System.out.println("Error: Out of range.");
            return selectPerson(list);
        }
        
        return list.get(choice - 1);
    }
    
    private void printPersonList(ArrayList<Person> list) {
        int i = 1;
        String format = "%d) %s";
        
        for(Person person : list) {
            System.out.println(String.format(format, i, person.getName()));
            i++;
        }
    }
    
    private void printPerson(Person person) {
        System.out.println("\n== Person oplysninger ==");
        System.out.println("Navn: " + person.getName());
        System.out.println("Adresse: " + person.getAddress());
        System.out.println("Postnummer: " + person.getZipCode());
        System.out.println("By: " + person.getCity());
        System.out.println("Telefonnummer: " + person.getPhone());
        System.out.println();
    }
    
    private void deletePerson(Person person) {
        if(controller.removePerson(person)) {
            System.out.println("Personen blev slettet");
        } else {
            System.out.println("Der opstod en fejl under fjernelsen af personen");
        }
    }
    
    public void editPerson(Person person) {
        
        boolean exit = false;
        
        String name;
        String address;
        int zipCode;
        String city;
        int phone;
        
        while(!exit) {

            printPerson(person);
            System.out.println("== Rediger person ==");
            System.out.println("" + EDIT_NAME + ") Rediger navn");
            System.out.println("" + EDIT_ADDRESS + ") Rediger adresse");
            System.out.println("" + EDIT_ZIPCODE + ") Rediger postnummer");
            System.out.println("" + EDIT_CITY + ") Rediger by");
            System.out.println("" + EDIT_PHONE + ") Rediger telefonnummer");
            System.out.println("" + EDIT_BACK + ") Tilbage til person menu");
            
            System.out.println("Vælg handling");
            
            int choice = inputInt();
            
            switch(choice) {
                
                case EDIT_NAME:
                    System.out.println("Indtast nyt navn");
                    name = inputString();
                    controller.editName(person, name);
                    break;
                    
                case EDIT_ADDRESS:
                    System.out.println("Indtast ny adresse");
                    address = inputString();
                    controller.editAddress(person, address);
                    break;
                    
                case EDIT_ZIPCODE:
                    System.out.println("Indtast nyt postnummer");
                    zipCode = inputInt();
                    controller.editZipCode(person, zipCode);
                    break;
                    
                case EDIT_CITY:
                    System.out.println("Indtast ny by");
                    city = inputString();
                    controller.editCity(person, city);
                    break;
                    
                case EDIT_PHONE:
                    System.out.println("Indtast nyt telefon nummer");
                    phone = inputInt();
                    controller.editPhone(person, phone);
                    break;
                    
                case EDIT_BACK:
                    exit = true;
                    break;
                
                default:
                    System.out.println("Ugyldigt valg");
                    break;
                
            }
        }
    }
    
    private void personMenu(Person person) {
        
        boolean exit = false;
        while(!exit) {
            
            printPerson(person);
            
            System.out.println("Vælg handling: ");
            System.out.println("" + PERSON_EDIT + ") Rediger person");
            System.out.println("" + PERSON_DELETE + ") Slet person");
            System.out.println("" + PERSON_BACK +") Tilbage til addressebogen");
            
            int choice = inputInt();
            
            switch(choice) {
                case PERSON_EDIT:
                    editPerson(person);
                    break;
                case PERSON_DELETE:
                    deletePerson(person);
                case PERSON_BACK:
                    exit = true;
                    break;
                default:
                    System.out.println("Indtast et gyldigt valg");
                    break;
            }
        }
        
    }
    
    private void createPerson() {
        
        System.out.println("== Opret person ==");
        
        System.out.println("Indtast navn");
        String name = inputString();
        
        System.out.println("\nIndtast addresse");
        String address = inputString();
        
        System.out.println("\nIndtast postnr");
        int zipCode = inputInt();
        
        System.out.println("\nIndtast by");
        String city = inputString();
        
        System.out.println("\nIndtast telefon nummer");
        int phone = inputInt();
        
        int id = controller.createPerson(name, address, zipCode, city, phone);
        System.out.println("Personen er oprettet.");
        
        Person person = controller.getPerson(id);
        personMenu(person);
    }
}
            
        
